package mapset;

import java.util.*;

/**
 * Created by Terry
 * Description:
 * User: GAOBO
 * Date: 2021-07-28
 * Time: 9:36
 */
public class TestDemo {

    public int singleNumber(int[] nums) {

        HashSet<Integer> set = new HashSet<>();

        for(int i = 0;i < nums.length;i++) {
            if(set.contains(nums[i])) {
                set.remove(nums[i]);
            }else{
                set.add(nums[i]);
            }
        }

        for(int i = 0;i < nums.length;i++) {
            if(set.contains(nums[i])) {
                return nums[i];
            }
        }
        return -1;
    }



    /*public Node copyRandomList(Node head) {
        if(head == null) return null;
        HashMap<Node,Node> map = new HashMap<>();
        Node cur = head;
        while(cur != null) {
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
     }
     */

    public int numJewelsInStones(String jewels, String stones) {
        stones.toUpperCase().toCharArray();
        HashSet<Character> set = new HashSet<>();

        for(int i = 0; i < jewels.length();i++) {
            set.add(jewels.charAt(i));
        }
        int count = 0;
        for(char ch : stones.toCharArray()) {
            if(set.contains(ch)) {
                count++;
            }
        }
        return count;
    }

    /*

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str1 = scan.nextLine();//期望的
        String str2 = scan.nextLine();//实际的

        HashSet<Character> setActual = new HashSet<>();
        for(char ch : str2.toUpperCase().toCharArray()) {
            setActual.add(ch);
        }

        HashSet<Character> setBroken = new HashSet<>();

        for(char ch : str1.toUpperCase().toCharArray()) {
            if(!setActual.contains(ch) && !setBroken.contains(ch)) {
                setBroken.add(ch);
                //这个ch就是坏了的
                System.out.print(ch);
            }
        }
    }
     */

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10_0000; i++) {
            list.add(random.nextInt(10000));
        }
        //        数据    次数
        HashMap<Integer,Integer> map = new HashMap<>();
        for (Integer key : list) {
            if(map.get(key) == null) {
                map.put(key,1);
            }else {
                //说明之前存储过
                int count = map.get(key);
                map.put(key,count+1);
            }
        }

        for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
            if(entry.getValue() > 1) {
                System.out.println("重复的数据: "+entry.getKey() +"出现的次数 : "+entry.getValue());
            }
        }
    }

    /**
     * 找到第一个重复的数据
     * @param args
     */
    public static void main5(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10_0000; i++) {
            list.add(random.nextInt(10000));
        }

        HashSet<Integer> set = new HashSet<>();

        for (int i = 0; i < list.size(); i++) {
            if(set.contains(list.get(i))) {
                System.out.println(list.get(i));
                break;
            }else {
                set.add(list.get(i));
            }
        }
    }


    /**
     * 去重
     * @param args
     */
    public static void main4(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10_0000; i++) {
            list.add(random.nextInt(10000));
        }

        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            set.add(list.get(i));
        }
        System.out.println(set.size());
        System.out.println(set);
    }


    public static void main3(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("bit1");
        set.add("bit2");
        set.add("bit3");
        set.add(null);
        set.add(null);
        System.out.println(set);

       Iterator<String> it =  set.iterator();

       while (it.hasNext()) {
           System.out.println(it.next());
       }

        System.out.println("=============");
        Object[] strings = set.toArray();
        //在Java当中  数组不能进行 整体的转换
        /*short[] array = {1,2,3,4};
        int[] array2 = (int[])array;*/
    }




    public static void main2(String[] args) {
        Map<String,String> map = new HashMap<>();
        //根据某种规则来存的 -》 hash表的实现的时候
        map.put("及时雨","宋江");
        map.put("国民女神","高圆圆");
        map.put("阿强","王宝强");
        map.put(null,"高博");
        map.put(null,"高博2");
        map.put(null,null);

        System.out.println(map);


        System.out.println("=================");

        Set<  Map.Entry<String, String> >  entrySet =  map.entrySet();

        for ( Map.Entry<String, String> entry   : entrySet) {
            System.out.println("key: "+entry.getKey() +" val " + entry.getValue());
        }

        System.out.println("=================");


        String val = map.get("国民女神");
        System.out.println(val);


        val = map.getOrDefault("国民女神2","如花");
        System.out.println(val);

        ///map.put("阿强","强强");//key值 一定是唯一的 如果key一样 那么val就更新

        System.out.println(map);//打印顺序 不一定是 put的顺序

        Set<String> stringSet = map.keySet();
        System.out.println(stringSet);


    }
}
